package demo.base;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class MyClient {

	public static void main(String[] args) {
		IoConnector connector = new NioSocketConnector();
		connector.setConnectTimeoutMillis(3000);
		connector.getFilterChain().addLast(
				"codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.WINDOWS
						.getValue(), LineDelimiter.WINDOWS.getValue())));
		connector.setHandler(new ClientHandler("aa bb"));
		
		/* 异步方法 */
		ConnectFuture future = connector.connect(new InetSocketAddress(9112));
        // 将异步执行转为同步执行，等待连接成功，
        future.awaitUninterruptibly();
//        future.getSession().close(false);
        /* IoSession的close()仅仅关闭了tcp的连接通道，并没有关闭server、client端的程序，需调用dispose方法停止 */
//		connector.dispose(true);
	}
}
